package com.xdf.zipperdye;

import com.fr.web.ReportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;

//@SpringBootApplication
//暂停使用 spring security 释放下面被注释的行
// 后台API地址：http://xdf206/zipperdye/swagger-ui.html
//@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@SpringBootApplication
public class ZipperdyeApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZipperdyeApplication.class, args);
    }

    // finereport 添加Servlet的映射
    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        return new ServletRegistrationBean(new ReportServlet(), "/ReportServer");
    }

    // finereport 设置内置Tomcat运行项目目录
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        // 设置帆软报表根目录（第三步中获取的绝对路径）
        // 这样设置对应最终的报表文件的路径是：D:\Java\finereport9.0\WebReport\WEB-INF\reportlets\gsms\采购入库单.cpt
        String documentRoot = "D:\\software\\FineReport9.0\\WebReport";

        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                if (factory instanceof TomcatServletWebServerFactory) {
                    TomcatServletWebServerFactory tomcat = (TomcatServletWebServerFactory) factory;
                    if (!StringUtils.isEmpty(documentRoot)) {
                        File root = null;
                        try {
                            root = ResourceUtils.getFile(documentRoot);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        tomcat.setDocumentRoot(root);
                    }
                }
            }
        };
    }
}
